'use strict';
var AKONG = window.AKONG || {};
AKONG.AutoPlay = function() {

}
AKONG.AutoPlay.prototype = {
	constructor: AKONG.AutoPlay,
	init: function(obj) {
		var self = this;
		self.scene = obj.scene;
		self.children = self.scene.children;
		self.length = self.children.length;
		self.maxIndex = self.length - 1;
		self.curIndex = obj.curIndex || 0;
		self.curIndex = Math.min(self.curIndex, self.maxIndex);
		self.prevIndex = self.curIndex;
		self.time = obj.time || 500;
		self.delayTime = obj.delayTime || 2000;
		self.autoplay = obj.autoplay || false;
		self.loop = typeof obj.loop === 'boolean' ? obj.loop : true;
		self.className = obj.className || 'hide';
		self.activeName = obj.activeName || false;
		self.type = obj.type || 0;
		self.orient = obj.orient || 0;
		self.canPlay = true;
		self.transformPrefix = 'transform' in document.body.style ? '' : '-webkit-';
		self.transitionPrefix = 'transition' in document.body.style ? '' : '-webkit-';
		self.animationPrefix = 'animation' in document.body.style ? '' : '-webkit-';
		self.animateName = obj.animateName || [];
		self.direction = 0;
		self.animate_id = null;
		self.state = 0;

		self.start = obj.start || false;
		self.callback = obj.callback || false;
		self.addEventListeners();
		if(self.autoplay) {
			self.play();
		}
	},
	addEventListeners: function() {
		var self = this;
		self.removeEventListeners();
		Array.prototype.forEach.call(self.children, function(v, i) {
			var s = v.style;
			if(self.type === 0) {
				s[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform 0s';
			}
			s[self.transformPrefix + 'transform'] = 'translate3d(0,0,0)';
			s[self.transformPrefix + 'backface-visibility'] = 'hidden';
			if(i !== self.curIndex) {
				v.classList.add(self.className);
			} else {
				v.classList.remove(self.className);
				if(self.activeName) {
					v.classList.add(self.activeName);
				}
			}
			if(self.type === 0) {
				v.addEventListener('transitionend', v.end = function(e) {
					e.stopPropagation();
					self.transitionEnd(e, v);
				});
				v.addEventListener('webkitTransitionEnd', v.end);
			} else {
				v.addEventListener('animationend', v.end = function(e) {
					e.stopPropagation();
					self.animationEnd(e, v);
				});
				v.addEventListener('webkitAnimationEnd', v.end);
			}
		});
	},
	removeEventListeners: function() {
		var self = this;
		if(self.children[0].end) {
			Array.prototype.forEach.call(self.children, function(v, i) {
				if(self.type === 0) {
					v.removeEventListener('transitionend', v.end);
					v.removeEventListener('webkitTransitionEnd', v.end);
				} else {
					v.removeEventListener('animationend', v.end);
					v.removeEventListener('webkitAnimationEnd', v.end);
				}
			});
		}
	},
	transitionEnd: function(e, v) {
		var self = this,
			that = v,
			children = self.children;
		if(that !== children[self.curIndex] || that !== e.target) {
			return;
		}
		children[self.prevIndex].classList.add(self.className);
		children[self.prevIndex].style[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform 0s';
		children[self.prevIndex].style[self.transformPrefix + 'transform'] = 'translate3d(0,0,0)';
		children[self.curIndex].style[self.transitionPrefix + 'transform'] = self.transformPrefix + 'transform 0s';

		self.end();
	},
	animationEnd: function(e, v) {
		var self = this,
			that = v,
			children = self.children;
		if(that !== children[self.curIndex] || that !== e.target) {
			return;
		}
		children[self.prevIndex].classList.add(self.className);
		children[self.curIndex].classList.remove(self.animateName[1]);
		children[self.prevIndex].classList.remove(self.animateName[0]);
		children[self.curIndex].classList.remove(self.animateName[3]);
		children[self.prevIndex].classList.remove(self.animateName[2]);
		self.end();
	},
	end: function() {
		var self = this;
		var children = self.children;
		if(self.activeName) {
			children[self.prevIndex].classList.remove(self.activeName);
			children[self.curIndex].classList.add(self.activeName);
		}
		self.canPlay = true;
		if(self.autoplay && self.state) {
			if(self.direction === 1) {
				self.animate_id = setTimeout(function() {
					self.next();
				}, self.delayTime);
			} else {
				self.animate_id = setTimeout(function() {
					self.prev();
				}, self.delayTime);
			}
		}
		self.callback && self.callback();
	},
	prev: function() {
		var self = this,
			index = self.curIndex,
			length = self.children.length;
		if(!self.canPlay || (!self.autoplay && !self.loop && index === 0)) {
			return;
		}
		if(self.state) {
			self.pause();
		}
		self.state = 2;
		if(self.start) {
			self.start();
		}
		self.prevIndex = index;
		index--;
		if(index < 0) {
			index += length;
		}
		self.curIndex = index % length;
		self.setIndex();
	},
	next: function() {
		var self = this,
			index = self.curIndex,
			length = self.children.length;
		if(!self.canPlay || (!self.autoplay && !self.loop && index === self.maxIndex)) {
			return;
		}
		if(self.state) {
			self.pause();
		}
		self.state = 1;
		if(self.start) {
			self.start();
		}
		self.prevIndex = index;
		index++;
		self.curIndex = index % length;
		self.setIndex();
	},
	play: function() {
		var self = this;
		self.pause();
		self.direction = 1;
		self.animate_id = setTimeout(function() {
			self.next();
		}, self.delayTime);
	},
	reverse: function() {
		var self = this;
		self.pause();
		self.direction = 2;
		self.animate_id = setTimeout(function() {
			self.prev();
		}, self.delayTime);
	},
	gotoAndPlay: function(index) {
		var self = this;
		self.gotoAndStop(index);
		self.play();
	},
	gotoAndReverse: function(index) {
		var self = this;
		self.gotoAndStop(index);
		self.reverse();
	},
	gotoAndStop: function(index) {
		var self = this;
		if(!self.canPlay) {
			return;
		}
		self.pause();
		if(index === self.curIndex) {
			return;
		}
		if(index > self.curIndex) {
			self.state = 1;
		} else {
			self.state = 2;
		}
		if(self.start) {
			self.start();
		}
		if(typeof index === 'number') {
			index = Math.min(index, self.maxIndex);
			self.prevIndex = self.curIndex;
			self.curIndex = index;
		}
		self.setIndex();
	},
	pause: function() {
		var self = this;
		self.state = 0;
		clearTimeout(self.animate_id);
	},
	setIndex: function(index) {
		var self = this,
			bol = typeof index === 'number';
		if(index === self.curIndex || !self.canPlay) {
			return;
		}
		if(bol) {
			if(self.state) {
				self.pause();
			}
			index = Math.min(index, self.maxIndex);
			self.prevIndex = self.curIndex;
			self.curIndex = index;
		}
		var time = self.time,
			children = self.children,
			state = self.state || (self.curIndex > self.prevIndex ? 1 : 2);
		self.canPlay = false;

		if(self.type === 0) {
			var c1S = children[self.prevIndex].style,
				c2S = children[self.curIndex].style,
				cur = '0,0,0',
				prev = self.orient ? '0,-100%,0' : '-100%,0,0',
				next = self.orient ? '0,100%,0' : '100%,0,0';
			if(!bol) {
				if(state === 1) {
					c2S[self.transformPrefix + 'transform'] = 'translate3d(' + next + ')';
				} else {
					c2S[self.transformPrefix + 'transform'] = 'translate3d(' + prev + ')';
				}
				children[self.curIndex].classList.remove(self.className);
				children[self.curIndex].offsetHeight;

				c1S[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform ' + time + 'ms';
				if(state === 1) {
					c1S[self.transformPrefix + 'transform'] = 'translate3d(' + prev + ')';
				} else {
					c1S[self.transformPrefix + 'transform'] = 'translate3d(' + next + ')';
				}
				c2S[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform ' + time + 'ms';
				c2S[self.transformPrefix + 'transform'] = 'translate3d(' + cur + ')';
			} else {
				children[self.prevIndex].classList.add(self.className);
				c1S[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform 0ms';
				c1S[self.transformPrefix + 'transform'] = 'translate3d(' + cur + ')';
				c2S[self.transitionPrefix + 'transition'] = self.transformPrefix + 'transform 0ms';

				children[self.curIndex].classList.remove(self.className);
				children[self.curIndex].offsetHeight;
				self.end();
			}
		} else {
			if(!bol) {
				children[self.curIndex].classList.remove(self.className);
				if(self.animateName.length === 2 || state === 1) {
					children[self.curIndex].classList.add(self.animateName[1]);
					children[self.prevIndex].classList.add(self.animateName[0]);
				} else {
					children[self.curIndex].classList.add(self.animateName[3]);
					children[self.prevIndex].classList.add(self.animateName[2]);
				}
			} else {
				children[self.prevIndex].classList.add(self.className);
				children[self.curIndex].classList.remove(self.className);
				self.end();
			}
		}
	}
}